<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="index.css" />
  </head>

  <body>
    <div id="app">
      <div class="container">
        <div class="form-group">
          <div class="input-group">
            <h4>品牌管理</h4>
          </div>
        </div>
        <table class="table table-bordered table-hover mt-2">
          <thead>
            <tr>
              <th>序号</th>
              <th>资产名称</th>
              <th>价格</th>
              <th>创建时间</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in list">
              <td>{{index + 1}}</td>
              <td>{{item.name}}</td>
              <td class="red">{{item.price}}</td>
              <td>{{item.time}}</td>
              <td><a @click.prevent="del(index)" href="#">删除</a></td>
            </tr>
          </tbody>
        </table>
        <form class="form-inline">
          <div class="form-group">
            <div class="input-group">
              <input
                v-model="name"
                type="text"
                placeholder="资产名称"
                class="form-control"
              />
            </div>
          </div>
          <p></p>
          <div class="form-group">
            <div class="input-group">
              <input
                v-model="price"
                type="text"
                placeholder="价格"
                class="form-control"
              />
            </div>
          </div>
          <p></p>
          <button class="btn btn-primary" @click.prevent="add">添加资产</button>
        </form>
      </div>
    </div>
    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          // 双向绑定输入的资产名字
          name: "",
          //双向绑定输入的价格
          price: "",
          list: [
            { name: "Gucci", price: 16000, time: new Date().toLocaleString() },
            { name: "可爱多", price: 25, time: new Date().toLocaleString() },
          ],
        },
        methods: {
          del(abc) {
            this.list.splice(abc, 1)
          },
          add() {
            //禁止默认行为

            //非空判断
            if (this.name == "" || this.price == "") return alert("不能为空")
            //添加到数组
            this.list.push({
              name: this.name,
              price: this.price,
              time: new Date().toLocaleString(),
            })
            //输入内容为空
            this.name = this.price = ""
          },
        },
      })
    </script>
  </body>
</html>
